home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / hc2_x / tcprogud.sit / TC Prog Guide / card_21830.txt < prev    next >
Text File  |  1991-02-27  |  1KB  |  25 lines

  1. -- card: 21830 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 4755
  5. -- name: 
  6.  
  7.  
  8. -- part contents for background part 4
  9. ----- text -----
  10. As of Think C 4.0, there is a confirmed bug which causes problems when an instance variable is an array of pointers, and the elements of this array are allocated dynamically using new().  In the author's experience this problem arises when a non-constant is used to specify the element of the array being allocated.
  11.  
  12. To avoid this problem it is sufficient to use a temporary variable such that the dynamic allocation takes place separately from assigning the new pointer to the array element.  The following example illustrates this technique.  It is assumed that student_array[] is an instance variable of class Room.
  13.  
  14.     int    Room::init(void)
  15.     {   int   i;
  16.         Student   *temp_student;
  17.         for (i=0 ; i<MAX_STUDENTS ; i++)
  18.         {   temp_student = new(Student);      /* use temp due to bug in Think C 4.0 */
  19.             student_array[i] = temp_student;    /* now it's ok to assign the pointer */
  20.         }
  21.     }
  22.  
  23. -- part contents for background part 7
  24. ----- text -----
  25. 127